// Pinger Program for PCMod (CLIENT)

PROG.PName = "FileViewer"
PROG.Author = "[GU]|ZLC|thomasfn"

PROG.NiceName = "File Viewer"

function PROG:BuildWindow( WindowName, Desktop, Object )
	local pn = vgui.Create( "DTree" )
	pn:SetParent( Object )
	pn:SetPos( 5, 35 )
	pn:SetSize( Object:GetWide()*0.25, Object:GetTall()-40 )
	self.TNS = {}
	self.TNS[ "root/" ] = pn:AddNode( "Hard Drive" )
	self.Tree = pn
	
	SendCommand( "read_structure", "/" )
	
	DInterface:AddButton( "btnQuit", Desktop, "X", Object:GetWide()-20, 0, 20, 20, function() SendCommand( "quit" ); end )
end

function PROG:ClearWindow( Desktop )
	if (self.Tree) then self.Tree:Remove(); self.Tree = nil; end
	
	DInterface:RemoveButton( "btnQuit", Desktop )
end

function PROG:StructureRecieved( um )
	local rdir = um:ReadString()
	local dir = "root" .. rdir
	local cnt = um:ReadShort()
	local par = self.TNS[ dir ]
	if (!par) then
		par = self.TNS[ "root/" ]
		Msg( "No node! (" .. dir .. ")\n" )
	end
	if (!par) then return end
	Msg( "Structure at '" .. dir .. "'\n" )
	local tmp = {}
	for i=1, cnt do
		local v = um:ReadString()
		// Msg( v .. ", " )
		local id = dir .. v .. "/"
		// If the node exists, skip
		local rid = rdir .. v .. "/"
		local pn = par:AddNode( v )
		self:FixupNode( pn, rid )
		self.TNS[ id ] = pn
		tmp[ i ] = id
		Msg( "Node Created! (" .. id .. ")\n" )
	end
	// Msg( "\n" )
end

function PROG:FilelistRecieved( um )
	local rdir = um:ReadString()
	local dir = "root" .. rdir
	local dir_ts = dir .. "/"
	local cnt = um:ReadShort()
	local par = self.TNS[ dir_ts ]
	Msg( "File list recieved! (" .. dir .. ")\n" )
	if (!par) then
		par = self.TNS[ "root/" ]
		Msg( "No node! (" .. dir_ts .. ")\n" )
	end
	if (!par) then return end
	Msg( tostring(cnt) .. " files recieved!\n" )
	for i=1, cnt do
		local v = um:ReadString()
		local id = dir .. v
		local pn = par:AddNode( v )
		if (pn.Icon) then pn.Icon:SetImage( "vgui/spawnmenu/file" ) end
		self.TNS[ id ] = pn
		Msg( v .. "\n" )
	end
end

function PROG:FixupNode( ctrl, id )
	ctrl.id = id
	function ctrl:GetChildNodes()
		local pn = self.ChildNodes
		if (!pn) then return {} end
		return pn:GetItems()
	end
	function ctrl:DoClick()
		// We need to get all children, and look up each one
		local chk = self.id
		local nodes = self:GetChildNodes()
		for k, v in pairs( nodes ) do
			local nid = v.id
			if (nid) then SendCommand( "read_structure", nid ) end
		end
	end
end

// Data[1] = Tag
// Data[2] = Content
function PROG:DataRecieved( Desktop, Data )
	
end

function PROG:Paint( x, y, w, h )
	// draw.SimpleText( self.NiceName, "SmallPCText", x + (w*0.05), y + (h*0.05), Color( 255, 255, 255 ), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP )
end

usermessage.Hook( "_pcmod_filestructure", function( um )
	if ((C_PRO) && (C_PRO.Exists) && (C_PRO.PName == "FileViewer")) then
		C_PRO:StructureRecieved( um )
	end
end )

usermessage.Hook( "_pcmod_filelist", function( um )
	if ((C_PRO) && (C_PRO.Exists) && (C_PRO.PName == "FileViewer")) then
		C_PRO:FilelistRecieved( um )
	end
end )